package com.android.hedgehog;

import java.util.List;

import android.graphics.Color;
import android.util.Log;

import com.android.hedgehog.RisingConnectionScreen.GameState;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input;
import com.android.hedgehog.framework.Input.TouchEvent;

public class FingerTrace {
	Graphics graphics;
	boolean[][][] XY;
	boolean[][] checkXY = new boolean[320][480];
	int px, py, nx, ny, xStep, yStep, xCounter, yCounter;
	float xtotdiff, ytotdiff, xStep2, yStep2, counter;
	boolean flag = false;
	boolean flag2 = false;
	boolean crossedOver = false;
	int currentXY = 0;
	int totalXY;
	int lastX;
	int lastY;
	int pawn = 0;
	int a, b;
	int x, y;
	int lastInt = 1;
	boolean right = true;
	boolean down = true;
	Input input;
	RisingConnectionScreen rcs;

	public FingerTrace(Input i, Graphics g, int matrices,
			RisingConnectionScreen rcs) {
		this.rcs = rcs;
		totalXY = matrices;
		XY = new boolean[totalXY][320][480];
		input = i;
		graphics = g;
	}

	public void traceLine() {
		List<TouchEvent> touchEvents = input.getTouchEvents();
		int len = touchEvents.size();

		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_DOWN
					|| event.type == TouchEvent.TOUCH_DRAGGED) {
				x = event.x;
				y = event.y;
				if (rcs.state == GameState.Phase2
						&& rcs.rns.choices[rcs.rns.listOrder - 1].xPosition < event.x
						&& rcs.rns.choices[rcs.rns.listOrder-1].xPosition + 40 > event.x
						&& rcs.rns.choices[rcs.rns.listOrder-1].yPosition < event.y
						&& rcs.rns.choices[rcs.rns.listOrder-1].yPosition + 40 > event.y) {
					rcs.state = GameState.Phase1;
				}
				if (rcs.state == GameState.Phase1) {
					line(event.x, event.y);
				}

			}// DOWN && DRAG

			if (event.type == TouchEvent.TOUCH_UP) {
				reFresh();
				flag = false;

			}// UP
		}
	}
	public void reFresh() {
		for (int i = 0; i < 320; i++) {
			for (int j = 0; j < 480; j++) {
				XY[currentXY][i][j] = false;
			}
		}
	}

	public void pause(boolean on) {
		flag2 = on;
	}

	public void line(int x, int y) {
		if (flag2)
			return;

		if (flag == false) {
			px = x;
			py = y;
			flag = true;
		} else {
			nx = x;
			ny = y;

			xtotdiff = ((float) (nx - px) / (float) Math.abs(ny - py));
			ytotdiff = ((float) (ny - py) / (float) Math.abs(nx - px));

			xStep = px;
			yStep = py;
			xStep2 = 0;
			yStep2 = 0;
			counter = 0;
			xCounter = 1;
			yCounter = 1;

			if (px == nx) {
				if (py < ny) {
					for (yStep = py; yStep < ny; yStep++)
						drawPoint(px, yStep);
				} else {
					for (yStep = ny; yStep < py; yStep++)
						drawPoint(px, yStep);
				}

			} else if (py == ny) {
				if (px < nx) {
					for (xStep = px; xStep < nx; xStep++)
						drawPoint(xStep, py);
				} else {
					for (xStep = nx; xStep < px; xStep++)
						drawPoint(xStep, py);
				}

			} else {

				if (xtotdiff > 0 && ytotdiff < 0) { // Q1
					while ((xStep <= nx && yStep >= ny)) {
						drawPoint(xStep, yStep);

						if (Math.abs(xtotdiff) >= counter)
							xStep2 += 0.2f;

						if (Math.abs(ytotdiff) >= counter)
							yStep2 -= 0.2f;

						if (Math.abs(xStep2) >= xCounter) {
							xStep++;
							xCounter++;
						}
						if (Math.abs(yStep2) >= yCounter) {
							yStep--;
							yCounter++;
						}

						counter += 0.2f;

						if (counter >= Math.abs(xtotdiff)
								&& counter >= Math.abs(ytotdiff))
							counter = 0;

					}

				} else if (xtotdiff < 0 && ytotdiff < 0) {// Q2
					while ((xStep >= nx && yStep >= ny)) {

						drawPoint(xStep, yStep);

						if (Math.abs(xtotdiff) >= counter)
							xStep2 -= 0.2f;

						if (Math.abs(ytotdiff) >= counter)
							yStep2 -= 0.2f;

						if (Math.abs(xStep2) >= xCounter) {
							xStep--;
							xCounter++;
						}
						if (Math.abs(yStep2) >= yCounter) {
							yStep--;
							yCounter++;
						}

						counter += 0.2f;

						if (counter >= Math.abs(xtotdiff)
								&& counter >= Math.abs(ytotdiff))
							counter = 0;

					}

				} else if (xtotdiff < 0 && ytotdiff > 0) {// Q3
					while ((xStep >= nx && yStep <= ny)) {
						drawPoint(xStep, yStep);

						if (Math.abs(xtotdiff) >= counter)
							xStep2 -= 0.2f;

						if (Math.abs(ytotdiff) >= counter)
							yStep2 += 0.2f;

						if (Math.abs(xStep2) >= xCounter) {
							xStep--;
							xCounter++;
						}
						if (Math.abs(yStep2) >= yCounter) {
							yStep++;
							yCounter++;
						}

						counter += 0.2f;

						if (counter >= Math.abs(xtotdiff)
								&& counter >= Math.abs(ytotdiff))
							counter = 0;

					}

				} else { // Q4
					while ((xStep <= nx && yStep <= ny)) {

						drawPoint(xStep, yStep);

						if (xtotdiff >= counter)
							xStep2 += 0.2f;

						if (ytotdiff >= counter)
							yStep2 += 0.2f;

						if (xStep2 >= xCounter) {
							xStep++;
							xCounter++;
						}
						if (yStep2 >= yCounter) {
							yStep++;
							yCounter++;
						}

						counter += 0.2f;

						if (counter >= xtotdiff && counter >= ytotdiff)
							counter = 0;

					}

				}

			}

			px = nx;
			py = ny;

		}// else

	}

	/**
	 * this method is called after traceLine() method to display the traced line
	 * on the screen. The parameters represent the area of the screen that will
	 * display the line.
	 * 
	 * (x + width) <320 AND (y+height) <480
	 * 
	 * 
	 * @param x
	 *            1st point on x-axis
	 * @param y
	 *            1st point on y-axis
	 * @param width
	 *            2nd point on x-axis
	 * @param height
	 *            2nd point on y-axis
	 * @param color
	 */
	public void drawLine(int x, int y, int width, int height, int color) {
		for (int h = 0; h < currentXY + 1; h++) {
			for (int i = x; i < x + width; i++) {
				for (int j = y; j < y + height; j++) {
					if (XY[h][i][j] == true) {
						graphics.drawPixel(i, j, color);
					}
				}
			}
		}

	}// drawLine

	public void drawPoint(int x, int y) {

		if (x > lastX)
			right = true;
		if (lastX > x)
			right = false;
		if (y > lastY)
			down = true;
		if (lastY > y)
			down = false;

		Log.d("direction", "down: " + down + "  :  right: " + right);

		for (int i = -1; i < 2; i++) {
			for (int j = -1; j < 2; j++) {
				XY[currentXY][x + i][y + j] = true;
				// checkXY[a][b++]=true;
			}
			// checkXY[a++][b]=true;
		}
		lastX = x;
		lastY = y;
	}
	public boolean ifHit(int x, int y, int width, int height) {
		for (int h = 0; h < currentXY + 1; h++) {
			for (int i = x; i < x + width; i++) {
				for (int j = y; j < y + height; j++) {
					if (XY[h][i][j] == true) {
						currentXY++;
						return true;
					}
				}
			}
		}
		return false;
	}

}
